package com.vf.admin.miaodong.controller;

import com.vf.admin.client.service.AppSignConfigService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppQianDaoRecord;
import com.vf.admin.miaodong.entity.AppSignConfig;
import com.vf.admin.miaodong.entity.AppTask;
import com.vf.admin.miaodong.entity.AppTaskRecord;
import com.vf.admin.miaodong.service.AfterQianDaoRecordService;
import com.vf.admin.miaodong.service.AfterTaskRecordService;
import com.vf.admin.miaodong.service.AfterTaskService;
import com.vf.admin.utils.StringEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.vf.admin.utils.StringEx.isNull;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AfterTaskController.java
 * @Description 后台管理任务
 * @createTime 2021年11月04日 14:49:00
 */
@Controller
@RequestMapping("/after/task")
public class AfterTaskController extends BaseController {

    @Autowired
    AfterTaskService afterTaskService;
    @Autowired
    AfterTaskRecordService afterTaskRecordService;
    @Autowired
    AfterQianDaoRecordService afterQianDaoRecordService;
    @Autowired
    AppSignConfigService signConfigService;

    /**
     * @Description 获取任务列表
     * @Param:
     * @return: java.lang.String
     * @Author: ljx
     * @Date: 2021/07/4
     */
    @RequestMapping("/list")
    public String list() {
        try {
            List<AppTask> appTaskList = afterTaskService.findList(null, null);
            request.setAttribute("list", appTaskList);
            return this.sucess("/web/after/task/list");
        } catch (Exception e) {
            e.printStackTrace();
            return this.error("系统繁忙");
        }

    }


    /**
     * @Description 获取任务详情
     * @Param:
     * @return: java.lang.String
     * @Author: ljx
     * @Date: 2021/07/4
     */
    @RequestMapping("/xiangqing")
    public String xiangqing(Integer id) {
        try {
            AppTask appTask = afterTaskService.getById(id);
            if (appTask == null) {
                return this.error("任务不存在");
            }
            request.setAttribute("appTask", appTask);
            return this.sucess("/web/after/task/edit");
        } catch (Exception e) {
            e.printStackTrace();
            return this.error("系统繁忙");
        }

    }


    /**
     * @Description 更新任务奖励
     * @Param:
     * @return: java.lang.String
     * @Author: ljx
     * @Date: 2021/07/4
     */
    /*@RequestMapping("/udpate")
    @ResponseBody
    public Result udpate(AppTask task) {
        try {
            if (task == null) {
                return this.errorJSON("任务为空");
            }
            if (isNull(task.getJinbi())) {
                return this.errorJSON("奖励数量不能为空");
            }

            if (task.getJinbi() <= 0) {
                return this.errorJSON("奖励数量必须大于0");
            }

            AppTask appTask = afterTaskService.getById(task.getId());
            if (appTask == null) {
                return this.errorJSON("任务不存在");
            }
            appTask.setJinbi(task.getJinbi());
            afterTaskService.saveOrUpdate(appTask);
            return this.sucessJSON("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("系统繁忙");
        }

    }*/


    /**
     * @Description 获取用户任务记录
     * @Param:
     * @return: java.lang.String
     * @Author: ljx
     * @Date: 2021/07/4
     */
    @RequestMapping("/recordlist")
    public String recordlist(SplitPageBean splitPageBean, String nick) {
        try {

            Map<String, Object> map = new HashMap<>();
            if (StringEx.isNotNull(nick)) {
                map.put("nick", nick);
            }
            List<AppTaskRecord> appTaskRecordList = afterTaskRecordService.findList(map, splitPageBean);
            request.setAttribute("list", appTaskRecordList);
            request.setAttribute("JiangliMap", Constants.getJiangliMap());
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/task/recordlist");
        } catch (Exception e) {
            e.printStackTrace();
            return this.error("系统繁忙");
        }

    }
    /**
     * @Description 获取用户签到记录
     * @Param:
     * @return: java.lang.String
     * @Author: ljx
     * @Date: 2021/07/4
     */
    @RequestMapping("/qiandaolist")
    public String qiandaolist(SplitPageBean splitPageBean, String nick) {
        try {

            Map<String, Object> map = new HashMap<>();
            if (StringEx.isNotNull(nick)) {
                map.put("nick", nick);
            }
            List<AppQianDaoRecord> appQianDaoRecordList = afterQianDaoRecordService.findList(map, splitPageBean);
            request.setAttribute("list", appQianDaoRecordList);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/task/qiandaolist");
        } catch (Exception e) {
            e.printStackTrace();
            return this.error("系统繁忙");
        }

    }

    @RequestMapping("/signConfigList")
    public String signConfigList(SplitPageBean splitPageBean) {
        try {
            List<AppSignConfig> appQianDaoRecordList = signConfigService.findList(splitPageBean);
            request.setAttribute("list", appQianDaoRecordList);
            request.setAttribute("splitPage", splitPageBean);
            return this.sucess("/web/after/task/signConfigList");
        } catch (Exception e) {
            e.printStackTrace();
            return this.error("系统繁忙");
        }

    }

    @RequestMapping("/signConfigInfo")
    public String signConfigInfo(Integer id) {
        try {
            AppSignConfig info = signConfigService.info(id);
            request.setAttribute("obj", info);
            return this.sucess("/web/after/task/signConfigEdit");
        } catch (Exception e) {
            e.printStackTrace();
            return this.error("系统繁忙");
        }
    }

    @RequestMapping("/signConfigUpdate")
    @ResponseBody
    public Result signConfigUpdate(AppSignConfig param) {
        try {
            AppSignConfig appSignConfig = signConfigService.infoByDayAndId(param.getDay(), param.getId());
            if(appSignConfig != null) {
                return errorJSON("当日已有配置，不能重复");
            }
            signConfigService.update(param);

            return sucessJSON("success");
        } catch (Exception e) {
            e.printStackTrace();
            return errorJSON("系统繁忙");
        }
    }


}